package org.ant.MyCalculate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.widget.Button;
import android.widget.TextView;

public class PointTool {
	private Button button;
	private TextView textView;
	private Boolean flag;
	
	PointTool(Button button ,TextView textView ,Boolean flag) {
		this.button = button;
		this.textView = textView;
		this.flag = flag;
	}
	boolean hasOperation(TextView textView){
		String str = textView.getText().toString();
		String regEx = "/+|/-|/*|//";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		boolean rs = m.find();
		return rs;
	}
	void dealPoint(Button button ,TextView textView){
		String contextText = textView.getText().toString();
		String lastChar = contextText.substring(contextText.length() - 1, contextText.length());
		String lastTwoChar = "";
		String notOrgrial = "";
		boolean hasOperation = hasOperation(textView);
		if(contextText.length() >= 2){
			lastTwoChar = contextText.substring(contextText.length() - 2, contextText.length());
		}
		hasOperation =  hasOperation(textView);
		int postion = contextText.contains("+")?contextText.indexOf("+"):
						(contextText.contains("-")?contextText.indexOf("-"):
							(contextText.contains("*")?contextText.indexOf("*"):
								(contextText.contains("/")?contextText.indexOf("/"):0)));
		String stringAfterOpeartion = contextText.substring(postion+1);
		if(hasOperation&&stringAfterOpeartion.contains(".")){
			return;
		}
		if(flag==false && lastTwoChar.equals(".0")){
			textView.setText("0.");
			return;
		}
		
		if(flag==false && lastTwoChar.contains(".")){
			textView.setText("0.");
			return;
		}
		
		if(lastChar!="+" && lastChar!="-"&&lastChar!="*"&&lastChar!="/"){
			if(button.getText().equals(".")){
				textView.setText((contextText + "0" + button.getText()).replace(" ", ""));
			}else{
				textView.setText((contextText + button.getText()).replace(" ", ""));
			}
			return;
		}else{
			textView.setText((contextText + "0.").replace(" ", ""));
			return;
		}
		
		
	}
	
	
}
